﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using BUS;
using Public;
using System.Security.Cryptography;

namespace QuanLyKhoHangVatTu
{
    public partial class ThemNguoiDung : DevExpress.XtraEditors.XtraForm
    {
        TaiKhoan_PB tk_pb = new TaiKhoan_PB();
        TaiKhoan_BUS tk_bus = new TaiKhoan_BUS();
        QuanLyNguoiDung qlnd = new QuanLyNguoiDung();

        string txtImage = "D:\\QLVT\\QuanLyKhoHangVatTu (5)\\QuanLyKhoHangVatTu\\QuanLyKhoHangVatTu\\Images\\noPhotoAvailable.jpg";
        byte[] imageData = null;

        public ThemNguoiDung()
        {
            InitializeComponent();
        }

        private void xtraTabControl1_Click(object sender, EventArgs e)
        {

        }

        private void ThemNguoiDung_Load(object sender, EventArgs e)
        {
            cboDonVi.Properties.DataSource = tk_bus.LayDSDonVi();
            cboDonVi.Properties.DisplayMember = "TENDONVI";
            cboDonVi.Properties.ValueMember = "MADONVI";

            if (Flag_PB.ThemSua == 1)
                EnableEditing(false);

            if (Flag_PB.ThemSua == 2)
            {

                EnableEditing(true);
                txtTenTaiKhoan.Text = TaiKhoan_PB.TenTaiKhoan;
                txtMK.Text = TaiKhoan_PB.MatKhau;
                txtXNMK.Text = TaiKhoan_PB.MatKhau;
                txtHoTen.Text = TaiKhoan_PB.HoTen;
                cboDonVi.EditValue = TaiKhoan_PB.DonVi;
                txtEmail.Text = TaiKhoan_PB.Email;
                pictureBox1.Image = QuanLyNguoiDung.myImage;
            }
        }
        private void EnableEditing(bool editing)
        {
            txtTenTaiKhoan.Properties.ReadOnly = editing;
            txtMK.Properties.ReadOnly = editing;
            txtXNMK.Properties.ReadOnly = editing;
        }

        private TaiKhoan_PB GetValues()
        {
            TaiKhoan_PB.TenTaiKhoan = txtTenTaiKhoan.Text;
            TaiKhoan_PB.MatKhau = txtMK.Text;
            TaiKhoan_PB.HoTen = txtHoTen.Text;
            TaiKhoan_PB.DonVi = cboDonVi.EditValue.ToString();
            TaiKhoan_PB.Email = txtEmail.Text;
            tk_pb.HinhAnh = imageData;
            return tk_pb;
        }
        private bool CheckNull()
        {
            if (txtTenTaiKhoan.Text == string.Empty)
            {
                txtTenTaiKhoan.ErrorText = "Tên tài khoản không được trống";
                return false;
            }
            if (txtMK.Text.Length < 6)
            {
                txtMK.ErrorText = "Phải nhập tối thiểu 6 ký tự";
                return false;
            }
            if (txtMK.Text.Length > 50)
            {
                txtMK.ErrorText = "Phải nhập tối đa 50 ký tự";
                return false;
            }
            if (txtMK.Text != txtXNMK.Text)
            {
                txtXNMK.ErrorText = "Xác nhận mật khẩu không trùng khớp";
                return false;
            }
            if (txtHoTen.Text == string.Empty)
            {
                txtHoTen.ErrorText = "Họ tên không được trống";
                return false;
            }
            if (cboDonVi.EditValue.ToString() == string.Empty)
            {
                cboDonVi.ErrorText = "Đơn vị không được trống";
                return false;
            }
            return true;
        }
        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPG|*.jpg|PNG|*.png|GIF|*.gif|All Files (*.*)|*.*";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                pictureBox1.ImageLocation = openFileDialog1.FileName;
                txtImage = openFileDialog1.FileName;
                openFileDialog1.Dispose();
            }
        }

        void AddImage(string fileName)
        {
            // Đọc file chuyển sang mảng
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                imageData = new Byte[fs.Length];
                fs.Read(imageData, 0, (int)fs.Length);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (CheckNull())
            {
                AddImage(txtImage);
                GetValues();
                TaiKhoan_PB.MatKhau = tk_pb.Encrypt(TaiKhoan_PB.MatKhau, true);
                if (Flag_PB.ThemSua == 1)
                    tk_bus.ThemTaiKhoan(tk_pb);
                if (Flag_PB.ThemSua == 2)
                    tk_bus.SuaTaiKhoan(tk_pb);
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}